package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.R;
import com.example.system.entity.AppointmentScheduler;
import com.example.system.entity.Doctor;
import com.example.system.entity.User;
import com.example.system.mapper.AppointmentSchedulerMapper;
import com.example.system.mapper.DoctorMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.service.IDoctorService;
import com.example.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2024-01-22
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AppointmentSchedulerMapper appointmentSchedulerMapper;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;



    @Override
    public R insert(Doctor doctor) {
        LocalDateTime createTime=LocalDateTime.now();
        User user=doctor.getUser();
        String encryptedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encryptedPassword);
        user.setCreateTime(createTime);
        user.setCreateBy(iUserService.getCurrentLoginUser().getUserName());
        user.setRole("ROLE_DOCTOR");
        if("0".equals(user.getSex())){
            user.setAvatar("http://localhost:8089/file/601709d766a5404c802885b7d95e0fee.jpg");
        }else if("1".equals(user.getSex())){
            user.setAvatar("http://localhost:8089/file/cce54ca715ae4a79bd3880d74c5cc670.jpg");
        }else{
            user.setAvatar("http://localhost:8089/file/0df5e1114946486d9879afa0e8c6cff7.jpg");
        }
        userMapper.insert(user);
        Integer userId=user.getUserId();
        doctor.setUserId(userId).setCreateBy(iUserService.getCurrentLoginUser().getUserName()).setCreateTime(createTime);
        doctorMapper.insert(doctor);
        return R.success();
    }

    @Override
    public R selectAll(int currentPage, int pageSize, String doctorName, String departmentName) {
        Page<Doctor> page=new Page<>(currentPage,pageSize);
        return R.success(doctorMapper.findByPage(page,doctorName,departmentName));
    }

    @Override
    public R deleteById(int id) {
       LocalDateTime deleteTime=LocalDateTime.now();
       LambdaQueryWrapper<Doctor> doctorLambdaQueryWrapper=new LambdaQueryWrapper<>();
       doctorLambdaQueryWrapper.eq(Doctor::getId,id);
       Integer userId=doctorMapper.selectOne(doctorLambdaQueryWrapper).getUserId();
       LambdaUpdateWrapper<User> userLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
       userLambdaUpdateWrapper.eq(User::getUserId,userId);
       userLambdaUpdateWrapper.set(User::getStatus,"0");
       userLambdaUpdateWrapper.set(User::getDelFlag,"1");
       userLambdaUpdateWrapper.set(User::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
       userLambdaUpdateWrapper.set(User::getUpdateTime,deleteTime);
       userMapper.update(new User(),userLambdaUpdateWrapper);
       return R.success();
    }

    @Override
    public R deleteByIds(int[] ids) {
        LocalDateTime deleteTime=LocalDateTime.now();
        Integer userId=null;
        LambdaQueryWrapper<Doctor> doctorLambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        for(Integer id:ids){
            doctorLambdaQueryWrapper.eq(Doctor::getId,id);
            userId=doctorMapper.selectOne(doctorLambdaQueryWrapper).getUserId();
            userLambdaUpdateWrapper.eq(User::getUserId,userId);
            userLambdaUpdateWrapper.set(User::getStatus,"0");
            userLambdaUpdateWrapper.set(User::getDelFlag,"1");
            userLambdaUpdateWrapper.set(User::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
            userLambdaUpdateWrapper.set(User::getUpdateTime,deleteTime);
            userMapper.update(new User(),userLambdaUpdateWrapper);
            doctorLambdaQueryWrapper.clear();
            userLambdaUpdateWrapper.clear();
        }
        return R.success();
    }

    @Override
    public R edit(Doctor doctor) {
        LocalDateTime updateTime=LocalDateTime.now();
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getUserId,doctor.getUserId());
        doctor.getUser().setUpdateBy(iUserService.getCurrentLoginUser().getUserName());
        doctor.getUser().setUpdateTime(updateTime);
        userMapper.update(doctor.getUser(),userLambdaUpdateWrapper);
        doctorMapper.updateById(doctor);
        return R.success();
    }

    @Override
    public R findAllDoctorNames() {
        return R.success(doctorMapper.findAllDoctorNames());
    }

    @Override
    public R selectDoctorSchedulerInfo(Integer userId,String currentMonth) {
        List<AppointmentScheduler> appointmentSchedulerList=appointmentSchedulerMapper.selectDoctorSchedulerInfo(userId,currentMonth);
        return R.success(appointmentSchedulerList);
    }


}
